/* ****************************************************************
   ****************************************************************
   * Com++ Professional (c) 2009 C++ World
   * http://www.cplusplusworld.com/ mailto:compp@cplusplusworld.com
   ****************************************************************
   ****************************************************************
*/


#ifndef  SOCKETTCP_H
#define  SOCKETTCP_H

#include <iostream>
#include <cstring>
#include <string>
#include <unistd.h>
#include <fcntl.h>

#include "ComPlusPlus/Socket.h"


namespace compp {

  /**
     Tcp Socket Class. Handling connection oriented processing.
  */

  class SocketTcp : public Socket {

  private:
    void NewTcpSocket();

  protected:

    bool              Async          ;
    bool              AsyncConnect   ;
    bool              OptTcpNoDelay  ;
    int               Backlog        ;   

  public:

    void SetOptTcpNoDelay( bool val) ;
    bool GetOptTcpNoDelay() const ;
    void SetOptAsyncConnect( bool val) ;
    bool GetOptAsyncConnect() const ;
    void SetOptAsync(bool val) ;
    bool GetOptSync() const;
    void SetBacklog(int val) ;
    bool GetBacklog() const ;
    virtual ~SocketTcp();

    SocketTcp( unsigned  int bufsize = SocketTcpStandardBufferSize );
    SocketTcp(const std::string & Addr, int port);
    SocketTcp( int port);
    SocketTcp ( struct sockaddr_in *saddr, int len, int fd, unsigned int readaheadbufsz = SocketTcpStandardBufferSize ) ;
    void Close ();
    void Shutdown (int method = 0);
    
    // Listen with backlog or stdbacklog
    void Listen ( const int backlog);
    void Listen ();

    // Do an explicit bind
    void Bind ( const std::string & addr, int port);
    void Bind ( const int port );

    bool Connect( const std::string &addr, const int port );
    SocketTcp* Accept ( void );

    // Nagle's algorithmn
    bool SetSockOptTcpNoDelay ( bool yesno ) ;
    // maximum segment size. cannot be more that MTU
    bool SetSockOptTCP_MAXSEG ( int val ) ;

    static const unsigned int SocketTcpStandardBufferSize = (8*1024);
    const std::string GetPeerSockIPAddr() const ;
    const unsigned short GetPeerSockPort() const;

    const std::string GetSockIPAddr() const ;
    const unsigned short GetSockPort() const;


  }; 
}
#endif
